package it.uniroma2.tesi.bellomaria;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Persona {
	
	int idPersona = 0;
	String nome_cognome = null;
	String ruolo = null;
	
	public String getNome_cognome() {
		return nome_cognome;
	}
	public void setNome_cognome(String nome_cognome) {
		this.nome_cognome = nome_cognome;
	}
	public int getIdPersona() {
		return idPersona;
	}
	public void setIdPersona(int idPersona) {
		this.idPersona = idPersona;
	}
	public String getRuolo() {
		return ruolo;
	}
	public void setRuolo(String ruolo) {
		this.ruolo = ruolo;
	}	

	public static int getMaxIdPersona(){
		Integer maxIdPersona = 0;

		Statement statement = null;
		ResultSet rs = null;
		try {
			// si esegue lo statement.
			statement = DBConnection.getConnection().createStatement();
			String query = new String("select max(p.idpersona) from persona p");
			rs = statement.executeQuery(query);
			while (rs.next()) {
				maxIdPersona = rs.getInt(1);
			}
		} catch (SQLException e) {
			System.out.println("SQLState : " + e.getSQLState());
			System.out.println("ErrorCode : " + e.getErrorCode());
			System.out.println("Message : " + e.getMessage());
			System.out.println("----------------------------------------");
		} finally {
			// rilascio delle risorse.
			try {
				rs.close();
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return maxIdPersona;
	}

	public static int findPersona(String nome, String ruolo){
		int idPersona = -1;

		Statement statement = null;
		ResultSet rs = null;
		try {
			// si esegue lo statement.
			statement = DBConnection.getConnection().createStatement();
			String query = new String("select * from persona p where p.nome_cognome='"+nome+"' and p.ruolo='"+ ruolo +"'");
			rs = statement.executeQuery(query);
			while (rs.next()) {
				idPersona = rs.getInt("idPersona");
			}
			
		} catch (SQLException e) {
			System.out.println("SQLState : " + e.getSQLState());
			System.out.println("ErrorCode : " + e.getErrorCode());
			System.out.println("Message : " + e.getMessage());
			System.out.println("----------------------------------------");
		} finally {
			// rilascio delle risorse.
			try {
				rs.close();
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return idPersona;
	}
}
